package com.knightcloud.micro.upms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knightcloud.common.base.vo.response.R;
import com.knightcloud.micro.upms.api.dto.SysUserDto;
import com.knightcloud.micro.upms.api.dto.SysUserInfoDto;
import com.knightcloud.micro.upms.api.entity.SysRole;
import com.knightcloud.micro.upms.api.entity.SysUser;
import com.knightcloud.micro.upms.service.ISysPermService;
import com.knightcloud.micro.upms.service.ISysRoleService;
import com.knightcloud.micro.upms.service.ISysUserService;
import com.knightcloud.micro.upms.service.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author knight
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

	/**
	 * 角色服务
	 */
	private final ISysRoleService roleService;

	/**
	 * 权限服务
	 */
	private final ISysPermService permService;

	@Nullable
	@Override
	public SysUser selectUserByUsername(String phone) {
		LambdaQueryWrapper<SysUser> queryWrapper = Wrappers.<SysUser>lambdaQuery()
			// 根据用户名过滤
			.eq(SysUser::getSuUsername, phone)
			.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	public R<SysUserDto> selectSysUserDtoByUsername(String username) {
		SysUser sysUser = this.selectUserByUsername(username);
		if (Objects.isNull(sysUser)) {
			return R.emptyData();
		}

		// 查询角色列表
		List<SysRole> sysRoles = roleService.selectRoleList(sysUser.getId());
		if (CollUtil.isEmpty(sysRoles)) {
			return R.vo(sysUser, SysUserDto::new);
		}

		Set<String> roleIds = sysRoles.stream().map(SysRole::getId).collect(Collectors.toSet());

		// 角色列表
		Set<String> roleSet = sysRoles.stream().map(SysRole::getSrKey).collect(Collectors.toSet());
		// 权限列表
		Set<String> permSet = permService.selectPermSetByRoleIds(roleIds);
		return R.vo(sysUser, user -> new SysUserDto(user, roleSet, permSet));
	}

	@Override
	public R<SysUserInfoDto> selectSysUserInfoDtoByUsername(String username) {
		LambdaQueryWrapper<SysUser> queryWrapper = Wrappers.<SysUser>lambdaQuery()
			.select(SysUser::getSuUsername, SysUser::getSuAvatar, SysUser::getSuName, SysUser::getSuGender,
					SysUser::getSuPhone, SysUser::getSuEmail, SysUser::getSuBirth)
			// 根据用户名过滤
			.eq(SysUser::getSuUsername, username)
			.eq(SysUser::getSuEnable, true)
			.eq(SysUser::getSuExpired, false)
			.eq(SysUser::getSuLocked, false)
			.eq(SysUser::getSuPasswordExpired, false)
			.last("limit 1");
		SysUser user = this.getOne(queryWrapper);
		if (ObjectUtil.isNull(user)) {
			return R.emptyData();
		}
		return R.vo(user, SysUserInfoDto::new);
	}

}
